// src/store/service/article.ts
import apiService from "@service/index";

const articleApiService = apiService
.enhanceEndpoints({ addTagTypes: ["article"] })
  .injectEndpoints({
    endpoints: (builder) => ({
     // 获取文章详情
     requestArticle: builder.query<ArticleResponse, string>({
      query: (id) => ({ url: `/articles/${id}` }),
      providesTags: ["article"],
    }),
    }),
  })
  .injectEndpoints({
    endpoints: (builder) => ({
      // 关注用户
      followUser: builder.mutation({
        query: (id: string|undefined) => ({
          url: "/user/followings",
          method: "POST",
          body: { target: id },
        }),
        invalidatesTags: ["article"],
      }),
      // 取消关注用户
      unFollowUser: builder.mutation({
        query: (id: string|undefined) => ({ url: `/user/followings/${id}`, method: "DELETE" }),
        invalidatesTags: ["article"],
      }),
    }),
  })
  .injectEndpoints({
    endpoints: (builder) => ({
      // 收藏文章
      collectArticle: builder.mutation({
        query: (id: string|undefined) => ({
          url: "/article/collections",
          method: "POST",
          body: { target: id },
        }),
        invalidatesTags: ["article"],
      }),
      // 取消收藏文章
      unCollectArticle: builder.mutation({
        query: (id: string|undefined) => ({
          url: `/article/collections/${id}`,
          method: "DELETE",
        }),
        invalidatesTags: ["article"],
      }),
    }),
  })
  .injectEndpoints({
    endpoints: (builder) => ({   
      // 点赞文章
      favouriteArticle: builder.mutation({
        query: (id: string|undefined) => ({
          url: "/article/likings",
          method: "POST",
          body: { target: id },
        }),
        invalidatesTags: ["article"],
      }),
      // 取消点赞文章
      unFavouriteArticle: builder.mutation({
        query: (id: string|undefined) => ({
          url: `/article/likings/${id}`,
          method: "DELETE",
        }),
        invalidatesTags: ["article"],
      }),
    }),
  });

export const { useRequestArticleQuery, useFollowUserMutation, useUnFollowUserMutation,useCollectArticleMutation, useUnCollectArticleMutation,useFavouriteArticleMutation, useUnFavouriteArticleMutation } = articleApiService;